package com.tanhua.dubbo.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tanhua.domain.db.UserInfo;
import com.tanhua.domain.vo.PageResult;
import com.tanhua.dubbo.api.db.UserInfoApi;
import com.tanhua.dubbo.mapper.UserInfoMapper;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

@DubboService
public class UserInfoApiImpl implements UserInfoApi {
    @Autowired
    private UserInfoMapper userInfoMapper;
    @Override
    public void save(UserInfo userInfo) {
        userInfoMapper.insert(userInfo);
    }

    @Override
    public void update(UserInfo userInfo) {
        userInfoMapper.updateById(userInfo);
    }

    @Override
    public UserInfo findById(Long id) {
        return userInfoMapper.selectById(id);
    }

    @Override
    public PageResult queryUsers(Integer page, Integer pagesize, Long id, String nickname, String city) {
        Page<UserInfo> page1 = new Page<>(page,pagesize);

        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        if(id!=null){
            queryWrapper.lambda().eq(UserInfo::getId,id);
        }
        if(StringUtils.isNotBlank(nickname)){
            queryWrapper.lambda().like(UserInfo::getNickname,"%"+nickname+"%");
        }
        if(StringUtils.isNotBlank(city)){
            queryWrapper.lambda().like(UserInfo::getCity,"%"+city+"%");
        }

        Page<UserInfo> userInfoPage = userInfoMapper.selectPage(page1, queryWrapper);

        return new PageResult(page,pagesize,(int)userInfoPage.getTotal(),userInfoPage.getRecords());
    }

    //

    @Override
    public UserInfo queryUserInfoById(Long id) {
        UserInfo userInfo = userInfoMapper.selectById(id);
        return userInfo;
    }
}
